/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Chatree Siangklom
 * Created On : Dec 21, 2012  11:47:49 AM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.std.batch.construction.highrise
 * File Name : ContractBatch.java
 * TODO
 */
package com.tis.std.batch.construction.highrise;

import java.util.List;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import promis.utility.QueueProcessAction;
import apps.sessionFactory;

import com.tis.std.commons;

/**
 * @author chatree
 *
 */
public class ContractBatch extends commons {
	
	private static final Logger log = Logger.getLogger(ContractBatch.class);

	private void BatchExec() throws Exception {
		try{
			
			StringBuilder _strQuery = new StringBuilder();
			_strQuery.append("  ");
			_strQuery.append(" SELECT ");
			_strQuery.append(" COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, LAST_UPD_BY,LAST_UPD_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON ");
			_strQuery.append(" FROM SPL.VW_SYNC_CONTRACTS_H_RISE ");
			_strQuery.append(" WHERE SYNC_ISSTAGING = 'N'  ");
			_strQuery.append(" AND STATUS IN ('A','Z','C','X') "); 
			_strQuery.append(" AND APPROVED_BY IS NOT NULL  ");
			_strQuery.append(" GROUP BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, LAST_UPD_BY,LAST_UPD_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON ");
			_strQuery.append(" ORDER BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, LAST_UPD_BY,LAST_UPD_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON ");
			
			List<Object[]> result = (List<Object[]>) sessionFactory.CreateEmPromis().createNativeQuery(_strQuery.toString()).getResultList();
			System.out.println("result size =" + CommonService.getSize(result));
			
			if(!CommonService.IsNull(result)){
				try{
					for (Object[] value : result) {	
						CleanValue();
						setAction(CommonService.IsNullRNull(value[3]));
						setCompany(CommonService.IsNullRNull(value[0]));
						setProject(CommonService.IsNullRNull(value[1]));
						setDocumentNo(CommonService.IsNullRNull(value[2]));
						setStatus(CommonService.IsNullRNull(value[3]));
						setSyncIsstaging(CommonService.IsNullRNull(value[4]));
						setProcessAction(QueueProcessAction.BATCH);
						setProcessActionBy(CommonService.IsNullRNull(value[9]));
						
						new apps.promis.out.common.SyncQueueAction().PutQueue(
																				promis.utility.BusinessModule.CONSTRUCTION_HIGH_RISE.toString(), 
																				promis.utility.ProcessModule.CONSTRUCTION_CONTRACT_HIGH_RISE.toString(),
																				null,
																				QueueProcessAction.BATCH,
																				ConvertParamToHashMap()
																			 );
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			throw e;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			
			new ContractBatch().BatchExec();
			
		}catch(Exception e){
			log.error(e.getMessage());
		}

	}

}
